#include"manage.h"
#include<stdlib.h>
#include<stdio.h>
#include<iomanip>

#include<string>
vector<book>ManageBook::bookList;
// int ManageBook::addBook(book &books)
// {
//     bookList.push_back(books);
//     return 0;
// }

int ManageBook::removeBook(const string &bookname)
{
    vector<book>::iterator iter = bookList.begin();
    while(iter != bookList.end())
    {
        if(bookname == (*iter).getBookName())
        {
            iter = bookList.erase(iter);
            return 0;
        }
        else
            ++iter;
    }
    return -1;
}

int ManageBook::alterBookInfor(const string &name)
{
    vector<book>::iterator iter = bookList.begin();
    while(iter != bookList.end())
    {
        if(name == (*iter).getBookName())
        {
            system("clear");
            cout<<"请选择要修改的书籍信息"<<endl;
            cout<<"---------------------------------------------------------------"<<endl;
            cout<<"\t\t\t0.书名"<<endl;
            cout<<"\t\t\t1.作者"<<endl;
            cout<<"\t\t\t2.出版社"<<endl;
            cout<<"\t\t\t3.定价"<<endl;
            cout<<"\t\t\t4.库存量"<<endl;
            cout<<"---------------------------------------------------------------"<<endl;
            cout<<"请输入你的选择:";
            int select = 0;
            string alterInfo;
            double alterInfo1;
            int number;
            cin>>select;
            getchar();
            switch(select)
            {
                case 1:
                    cout<<"输入修改后的书籍作者:";
                    getline(cin,alterInfo);
                    (*iter).m_autor = alterInfo;
                    break;
                case 2:
                    cout<<"输入修改后的书籍出版社:";
                    getline(cin,alterInfo);
                    (*iter).m_press = alterInfo;
                    break;
                case 3:
                    cout<<"输入修改后的书籍定价:";
                    cin>>alterInfo1;
                    (*iter).m_price = alterInfo1;
                    break;
                case 4:
                    cout<<"输入修改后的书籍库存量:";
                    cin>>number;
                    (*iter).m_number = number;
                    break;
                case 0:
                    cout<<"输入修改后的书籍名字:";
                    getline(cin,alterInfo);
                    (*iter).m_name = alterInfo;
                    break;
                
            }
            return 0;    
        }
        else
            ++iter;
        
    }
    cout<<"没有这本书！请直接添加"<<endl;

    return 0;
}

int ManageBook::showBookInfor()
{
    vector<book>::iterator iter = bookList.begin();
    while(iter != bookList.end())
    {
        if(iter == bookList.begin())
        {
            cout<<left<<setw(20)<<"书名"<<left<<setw(20)<<"作者"<<left<<
            setw(20)<<"出版社"<<left<<setw(10)<<"定价"<<left<<setw(5)<<"现有库存"<<endl;
        }
        cout<<(*iter)<<endl;
        ++iter;
    }

    return 0;
}

vector<book>::iterator ManageBook::findBook(const string&name)
{
    vector<book>::iterator iter = bookList.begin();
    while(iter != bookList.end())
    {
        if((*iter).m_name == name)
            break;
        ++iter;
    }
    return iter;

}
vector<user>ManageUser::userList;

int ManageUser::removeuser(user &u)
{
    return 0;
}

int ManageUser::alterUserInfor(user &u)
{
    cout<<"因系统的限制,您只能对您的电话和寄送地址进行修改!!!\n如想修改其他信息,请联系网购图书系统的官方人员"<<endl;
    cout<<"请输入修改信息:";
    getchar();
    string alterInfor;
    getline(cin,alterInfor);
    if(isdigit(alterInfor[0]))
        u.m_phone = alterInfor;
    else 
        u.m_address = alterInfor;
    return 0;
}

int ManageUser::showuserInfor()
{
    vector<user>::iterator iter = userList.begin();
    while(iter != userList.end())
    {
        if(iter == userList.begin())
            cout<<left<<setw(11)<<"账号"<<left<<setw(11)<<" 姓名"
            <<left<<setw(15)<<" 电话"<<left<<setw(10)
            <<" 寄送地址"<<left<<setw(10)<<"账户余额"<<endl;
        cout<<(*iter)<<endl;
        ++iter;
    }
    return 0;
}
int ManageUser::findMaxID()
{
    int maxID = 1000;
    vector<user>::iterator iter = userList.begin();
    while(iter != userList.end())
    {
        maxID = (*iter).m_ID>maxID?(*iter).m_ID:maxID;
        ++iter;
    }
    return maxID;

}

// void purchase(user &u)
// {
//     system("clear");
//     cout<<"\n\n\n\t\t欢迎尊敬的会员"<<u.m_name<<",您来到网购图书系统"<<endl;
//     cout<<"---------------------------------------------------------------"<<endl;
//     cout<<"请输入你想要购买的书籍名字：";
//     string name;
//     cin>>name;
//     vector<book>::iterator it = Man_book.findBook(name);
//     if(it == bookList.end())
//     {    cout<<"没有这本书,真可信!!!!"<<endl;
//         return ;
//     }
//     (*it).m_number--;
//     //purchaseRecord(string nameOfuser,string nameOfBook,char *purchaseTime,double cost);
//     char purchaseTime[20] = {'\0'};
//     strcmp(purchaseTime ,"2017-01-01 12:12:12");
//     purchaseRecord temp(u.m_name,(*it).m_name,purchaseTime,(*it).m_price);
//     Man_PurchaseR.addPurchaseRecord(temp);
//     cout<<"---------------------------------------------------------------"<<endl;
    
// }

vector<user>::iterator ManageUser::findUser( int ID)
{
    vector<user>::iterator iter =userList.begin();
    while(iter != userList.end())
    {
        if((*iter).m_ID == ID)
            break;
        ++iter;
    }
    return iter;
}

vector<purchaseRecord>ManagePurchaseR::purchaseRecordList;

int ManagePurchaseR::removePurchaseRecord(purchaseRecord &p)
{
    return 0;
}
int ManagePurchaseR::alterPurchaseRecordInfor(purchaseRecord &p)
{
    return 0;
}
int ManagePurchaseR::showPurchaseRecordInfor()
{
    vector<purchaseRecord>::iterator iter = purchaseRecordList.begin();
    while(iter != purchaseRecordList.end())
    {
        if(iter == purchaseRecordList.begin())
            cout<<left<<setw(10)<<"用户姓名"<<left<<setw(10)<<"购买的书名"<<
            left<<setw(10)<<"购买时间"<<left<<setw(10)<<"花费"<<endl;
        cout<<(*iter)<<endl;
        ++iter;
    }
    return 0;
}
int ManagePurchaseR::showPurchaseRecordInfor(user &u)
{
    vector<purchaseRecord>::iterator iter = purchaseRecordList.begin();
    while(iter != purchaseRecordList.end())
    {
        if((*iter).m_nameOfuser == u.m_name)
            cout<<(*iter)<<endl;;
        ++iter;
    }
    return 0;
}